package com.school.repair.controller;

import com.google.zxing.WriterException;
import com.school.repair.entity.*;
import com.school.repair.service.*;
import com.school.repair.util.AjaxResult;
import com.school.repair.util.Page;
import com.school.repair.util.QRCodeUtil;
import com.school.repair.util.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;

@RestController
public class BaoXiuManController {
    @Autowired
    BaoXiuManService baoXiuManService;

    @Autowired
    EquipmentService equipmentService;

    @Autowired
    RepairManService repairManService;

    @Autowired
    BaoXiuDanService bs;

    @Autowired
    TodoService ts;

    @GetMapping(value = "BaoXiuManlogin", produces = "application/json;charset=utf-8")
    public AjaxResult login(BaoXiuMan baoXiuMan, HttpSession session){
        String name=baoXiuMan.getCname();
        String pass=baoXiuMan.getPass();
        BaoXiuMan bxm=baoXiuManService.login(name, pass);
        if(bxm!=null){
            session.setAttribute("login","666");
            return AjaxResult.success(bxm);
        }
        return AjaxResult.error("用户名或者密码不正确！");
    }

    @RequestMapping(value = "equipmentPaging", produces = "application/json;charset=utf-8")
    public Page<Equipment> equipmentPaging(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int pageSize,
                                         @RequestParam(name = "createdate") Date createdate,@RequestParam(name = "name") String name,@RequestParam(name = "id") long id){

        return equipmentService.equipmentPaging(pageNo, pageSize, createdate, name, id);
    }

    @RequestMapping(value = "equipmentSearch", produces = "application/json;charset=utf-8")
    public Page<Equipment> equipmentSearch(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int pageSize,
                                           @RequestParam(name = "equipmentType") long equipmentType,@RequestParam(name = "name") String name,@RequestParam(name = "id") long id){
        return equipmentService.equipmentSearch(pageNo, pageSize, equipmentType, name, id);
    }

    @RequestMapping(value = "baoxiumanAddEquipment", produces = "application/json;charset=utf-8")
    public AjaxResult baoxiumanAddEquipment(@ModelAttribute(name = "equipment")Equipment equipment){
        int r=equipmentService.unitAddEquipment(equipment);
        if(r==0){
            return AjaxResult.error("增加失败");
        }
        return AjaxResult.success("增加成功");
    }

//  拿到所有的工程师并进行分页
    @RequestMapping(value = "repairManserch", produces = "application/json;charset=utf-8")
    public Page<RepairMan> repairManserch(@RequestParam(name = "page", defaultValue = "1") int pageNo, @RequestParam(name = "limit", defaultValue = "10") int pageSize,
                                          @RequestParam(name = "name") String name,@RequestParam(name = "goodat") String goodat){
        return repairManService.repairManPage(pageNo, pageSize, name, goodat);
    }

    @RequestMapping(value = "dorepair", produces = "application/json;charset=utf-8")
    public AjaxResult dorepair(@RequestParam(name = "guzhang")String guzhang,@RequestParam(name = "status")int status,@RequestParam(name = "bao_xiu_man_id")long bao_xiu_man_id,@RequestParam(name = "equipment_id")int equipment_id,
                               @RequestParam(name = "repair_man_id")long repair_man_id,@RequestParam(name = "repair_res")String repair_res,@RequestParam(name = "fee")float fee){
        Todo td = new Todo();
        td.setEquipmentId(equipment_id);
        Date date = new Date();
        String dt = Root.dateToStr(date);
        date = Root.toDate(dt);
        td.setCreatedate(date);
        BaoXiuDan bxd = new BaoXiuDan(guzhang, status, bao_xiu_man_id, equipment_id, repair_man_id, repair_res, fee);
        System.out.println(bxd);
        int r = 0;
        try {
            r = bs.add(bxd);
        } catch (Exception e) {
        }
        if (r == 0) {
            return AjaxResult.error("申报失败");
        }
        //添加一条 TODO数据
        ts.add(td);
        return AjaxResult.success("申报成功");
    }

    //生成二维码
    @RequestMapping(value = "addQrcode", produces = "application/json;charset=utf-8")
    public AjaxResult addQrcode(@RequestParam("id") long id, HttpSession session)throws IOException, WriterException {
        ServletContext application = session.getServletContext();
        OutputStream output = new FileOutputStream(application.getRealPath("images") + "/" + id + ".jpg");
        QRCodeUtil.genGR("https://dhrweb.utools.club/equipmentUpdate.html?" + id, output);
        String barCode = id + "jpg";
        int a = equipmentService.addBarCode(id, barCode);
        if (a > 0) {
            return AjaxResult.success("生成成功");
        }
        return AjaxResult.error("生成失败");
    }

    //添加一条反馈
    @RequestMapping(value = "addcomment", produces = "application/json;charset=utf-8")
    public AjaxResult addcomment(@RequestParam(name = "baoxiudan_id")long baoxiudan_id,@RequestParam(name = "fee")double fee,@RequestParam(name = "score")double score,@RequestParam(name = "repair_res")String repair_res,@RequestParam(name = "content")String content){
        System.out.println("添加一条反馈");
        Comment c = new Comment(content,score,baoxiudan_id);
        int r = 0;
        try{
            r=bs.update(baoxiudan_id,repair_res,fee);
            CommentService cs=new CommentService();
            r = cs.add(c);
        }catch (Exception e){}
        if (r!=0){
            return AjaxResult.success("反馈成功");
        }
        return AjaxResult.error("反馈失败");
    }

    @RequestMapping(value = "getEquipmentPic", produces = "application/json;charset=utf-8")
    protected AjaxResult getEquipmentPic(@RequestParam("id") long id) {
        EquipmentPicService equipmentPicService=new EquipmentPicService();
        EquipmentPic equipmentPic = equipmentPicService.getEquipmentPic(id);
        if (equipmentPic != null)
            return AjaxResult.success(equipmentPic);
        return AjaxResult.error("此设备无照片");
    }

    @RequestMapping(value = "addEQPic", produces = "application/json;charset=utf-8")
    protected AjaxResult addEQPic(EquipmentPic equipmentPic) {
        EquipmentPicService equipmentPicService=new EquipmentPicService();
        equipmentPicService.delPic(equipmentPic.getEquipmentId());
        int r = equipmentPicService.add(equipmentPic);
        if (r > 0) {
            return AjaxResult.success("添加成功");
        }
        return AjaxResult.error("此设备无照片");
    }
}

